package com.zh.common.view

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.view.isVisible
import com.blankj.utilcode.util.SizeUtils
import com.zh.common.R
import com.zh.common.ktx.dp2px
import com.zh.common.ktx.inflate
import com.zh.common.ktx.sp2px
import com.zh.common.utils.DoubleUtils

/**
 * 自定义标题控件
 */
class TitleBarView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyle: Int = 0
) : FrameLayout(context, attrs, defStyle) {

    private val view by lazy {
        context.inflate(R.layout.title_bar_layout)
    }

    private val mLeftImage by lazy { view.findViewById<ImageView>(R.id.title_bar_left_image) }
    private val mRightImage by lazy { view.findViewById<ImageView>(R.id.title_bar_right_image) }
    private val mRightImage2 by lazy { view.findViewById<ImageView>(R.id.title_bar_right_image2) }
    private val mCenterTextView by lazy { view.findViewById<TextView>(R.id.title_bar_center_text) }
    private val mRightTextView by lazy { view.findViewById<TextView>(R.id.title_bar_right_text_view) }
    private val mLeftTextView by lazy { view.findViewById<TextView>(R.id.title_bar_left_text_view) }
    private val mTitleBar by lazy { view.findViewById<RelativeLayout>(R.id.rl_title_bar_layout) }
    private val mDividerView by lazy { view.findViewById<View>(R.id.title_bar_divider) }//下划线

    private var mCenterString: String? = null// 标题文字
    private var mCenterColor = Color.WHITE // 标题颜色
    private var mCenterTextSize: Float? = SizeUtils.sp2px(14f).toFloat() // 标题文字大小 = 0f
    private var mRightString: String? = null//右边文字
    private var mRightColor = Color.WHITE // 右边文字颜色
    private var mRightTextSize: Float? = SizeUtils.sp2px(14f).toFloat() // 右边文字大小 = 0f
    private var mLeftString: String? = null//左边文字
    private var mLeftColor = Color.WHITE // 左边文字颜色
    private var mLeftTextSize: Float? = SizeUtils.sp2px(14f).toFloat() // 左边文字大小 = 0f
    private var isShowDivider: Boolean? = false //是否显示分界线 = false
    private var mTitleBarBackground = Color.GREEN //标题栏背景色
    private var mTitleBarHeight: Float? = SizeUtils.dp2px(45f).toFloat() //标题栏高度 = 0f
    private var mLeftDrawable: Drawable? = null//左边图片
    private var mRightDrawable: Drawable? = null//右边图片
    private var mRightDrawable2: Drawable? = null//右边图片2

    init {
        removeAllViews()
        addView(view)
        // Load attributes
        val typedArray =
            context.obtainStyledAttributes(attrs, R.styleable.TitleBarView, defStyle, 0)

        // 标题文字属性
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_centerText)) {
            mCenterString = typedArray.getString(R.styleable.TitleBarView_tb_centerText)
        }
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_centerTextColor)) {
            mCenterColor =
                typedArray.getColor(R.styleable.TitleBarView_tb_centerTextColor, mCenterColor)
        }
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_centerTextSize)) {
            mCenterTextSize =
                typedArray.getDimension(
                    R.styleable.TitleBarView_tb_centerTextSize, 14f.sp2px.toFloat()
                )
        }

        //右边文字属性
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_rightText)) {
            mRightString = typedArray.getString(R.styleable.TitleBarView_tb_rightText)
        }
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_rightTextColor)) {
            mRightColor =
                typedArray.getColor(R.styleable.TitleBarView_tb_rightTextColor, mRightColor)
        }
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_rightTextSize)) {
            mRightTextSize =
                typedArray.getDimension(
                    R.styleable.TitleBarView_tb_rightTextSize, 14f.sp2px.toFloat()
                )
        }

        //左边文字属性
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_leftText)) {
            mLeftString = typedArray.getString(R.styleable.TitleBarView_tb_leftText)
        }
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_leftTextColor)) {
            mLeftColor = typedArray.getColor(R.styleable.TitleBarView_tb_leftTextColor, mLeftColor)
        }
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_leftTextSize)) {
            mLeftTextSize =
                typedArray.getDimension(
                    R.styleable.TitleBarView_tb_leftTextSize, 14f.sp2px.toFloat()
                )
        }

        //分界线
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_divider)) {
            isShowDivider = typedArray.getBoolean(R.styleable.TitleBarView_tb_divider, false)
        }
        mDividerView?.isVisible = isShowDivider as Boolean

        //TitleBar属性
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_titleBarBackground)) {
            mTitleBarBackground =
                typedArray.getColor(
                    R.styleable.TitleBarView_tb_titleBarBackground, mTitleBarBackground
                )
        }
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_titleBarHeight)) {
            mTitleBarHeight =
                typedArray.getDimension(R.styleable.TitleBarView_tb_titleBarHeight, 0f)
        }

        //左边图片是否可见
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_leftImageDrawable)) {
            mLeftDrawable = typedArray.getDrawable(R.styleable.TitleBarView_tb_leftImageDrawable)
            setLeftImage()
        } else {
            mLeftImage?.isVisible = false
            if (mLeftImage?.drawable?.current?.constantState != null) {
                setLeftImage()
            }
        }

        //右边图片是否可见
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_rightImageDrawable)) {
            mRightDrawable = typedArray.getDrawable(R.styleable.TitleBarView_tb_rightImageDrawable)
            mRightImage?.apply {
                this.setImageDrawable(mRightDrawable)
                this.isVisible = true
            }
        } else {
            mRightImage?.isVisible = false
        }

        //右边图片是否可见2
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_rightImageDrawable2)) {
            mRightDrawable2 =
                typedArray.getDrawable(R.styleable.TitleBarView_tb_rightImageDrawable2)
            mRightImage2?.apply {
                this.setImageDrawable(mRightDrawable2)
                this.isVisible = true
            }
        } else {
            mRightImage2?.isVisible = false
        }

        //右边图片距离右边距离
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_rightImage2_marginRight)) {
            setRightImage2MarginRight(
                typedArray.getDimension(
                    R.styleable.TitleBarView_tb_rightImage2_marginRight,
                    0f
                )
            )
        }

        //右边文字是否可见
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_rightText)) {
            mRightTextView?.apply {
                this.text = mRightString
                this.isVisible = true
            }
            if (typedArray.hasValue(R.styleable.TitleBarView_tb_rightTextColor)) {
                setRightTextColor(mRightColor)
            }
            if (typedArray.hasValue(R.styleable.TitleBarView_tb_rightTextSize)) {
                mRightTextSize?.let { setRightTextSize(it) }
            }
        } else {
            mRightTextView?.isVisible = false
        }

        //左边文字是否可见
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_leftText)) {
            mLeftTextView?.apply {
                this.text = mLeftString
                this.isVisible = true
                this.setOnClickListener {
                    if (DoubleUtils.isFastDoubleClick(mLeftTextView)) return@setOnClickListener
                    (getContext() as Activity).onBackPressed()
                }
            }
            if (typedArray.hasValue(R.styleable.TitleBarView_tb_leftTextColor)) {
                setLeftTextColor(mLeftColor)
            }
            if (typedArray.hasValue(R.styleable.TitleBarView_tb_leftTextSize)) {
                mLeftTextSize?.let { setLeftTextSize(it) }
            }
        } else {
            mLeftTextView?.isVisible = false
        }

        //设置标题文字
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_centerText)) {
            setCenterText(mCenterString)
            if (typedArray.hasValue(R.styleable.TitleBarView_tb_centerTextColor)) {
                setCenterColor(mCenterColor)
            }
            if (typedArray.hasValue(R.styleable.TitleBarView_tb_centerTextSize)) {
                mCenterTextSize?.let { setCenterTextSize(it) }
            }
        }

        //设置TitleBar
        mTitleBarHeight?.let { setTitleBarHeight(it) }
        if (typedArray.hasValue(R.styleable.TitleBarView_tb_titleBarBackground)) {
            setTitleBarBackground(mTitleBarBackground)
        }
        typedArray.recycle()
    }

    private fun setLeftImage() {
        mLeftImage?.apply {
            isVisible = true
            if (mLeftDrawable != null) setImageDrawable(mLeftDrawable)
            else setImageDrawable(mLeftImage?.drawable)
            setOnClickListener {
                if (DoubleUtils.isFastDoubleClick()) return@setOnClickListener
                (context as Activity).onBackPressed()
            }
        }
    }

    /**
     * 左边图片点击事件
     *
     * @param listener
     */
    fun setLeftImageClickListener(listener: OnClickListener?) {
        mLeftImage?.setOnClickListener(listener)
    }

    /**
     * 右边图片点击事件
     *
     * @param listener
     */
    fun setRightImageClickListener(listener: OnClickListener?) {
        mRightImage?.setOnClickListener(listener)
    }

    /**
     * 右边图片点击事件2
     *
     * @param listener
     */
    fun setRightImage2ClickListener(listener: OnClickListener?) {
        mRightImage2?.setOnClickListener(listener)
    }

    /**
     * 右边图片2_距离右边距离
     *
     * @param right
     */
    fun setRightImage2MarginRight(right: Float) {
        (mRightImage2?.layoutParams as LayoutParams).rightMargin = right.toInt()
    }

    /**
     * 右边文件点击事件
     *
     * @param listener
     */
    fun setRightTextClickListener(listener: OnClickListener?) {
        mRightTextView?.setOnClickListener(listener)
    }

    /**
     * 左边文字点击事件
     *
     * @param listener
     */
    fun setLeftTextClickListener(listener: OnClickListener?) {
        mLeftTextView?.setOnClickListener(listener)
    }

    /**
     * 设置左边文字是否可见
     *
     * @param visibility
     */
    fun setLeftTextVisibility(visibility: Int) {
        mLeftTextView?.visibility = visibility
    }

    /**
     * 设置左边文字
     *
     * @param text
     */
    fun setLeftText(text: String?) {
        mLeftTextView?.text = text
    }

    /**
     * 设置左边文字颜色
     *
     * @param color
     */
    fun setLeftTextColor(color: Int) {
        mLeftTextView?.setTextColor(color)
    }

    /**
     * 设置左边文字大小
     *
     * @param size
     */
    fun setLeftTextSize(size: Float) {
        mLeftTextView?.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
    }

    /**
     * 设置右边文字
     *
     * @param text
     */
    fun setRightText(text: String?) {
        mRightTextView?.text = text
    }

    /**
     * 设置右边文字颜色
     *
     * @param color
     */
    fun setRightTextColor(color: Int) {
        mRightTextView?.setTextColor(color)
    }

    /**
     * 设置右边文字大小
     *
     * @param size
     */
    fun setRightTextSize(size: Float) {
        mRightTextView?.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
    }

    /**
     * 设置右边文字是否可见
     *
     * @param visibility
     */
    fun setRightTextVisibility(visibility: Int) {
        mRightTextView?.visibility = visibility
    }

    /**
     * 获取右边TextView
     */
    fun getRightTextView() = mRightTextView

    /**
     * 设置右边的图片是否可见
     *
     * @param visibility
     */
    fun setRightImageVisibility(visibility: Int) {
        mRightImage?.visibility = visibility
    }

    /**
     * 设置右边的图片是否可见2
     *
     * @param visibility
     */
    fun setRightImage2Visibility(visibility: Int) {
        mRightImage2?.visibility = visibility
    }

    /**
     * 设置右边图片
     *
     * @param resource
     */
    fun setRightImageResource(resource: Int) {
        mRightImage?.setImageResource(resource)
    }

    /**
     * 设置右边图片2
     *
     * @param resource
     */
    fun setRightImage2Resource(resource: Int) {
        mRightImage2?.setImageResource(resource)
    }

    /**
     * 设置左边的图片是否可见
     *
     * @param visibility
     */
    fun setLeftImageVisibility(visibility: Int) {
        mLeftImage?.visibility = visibility
    }

    /**
     * 设置左边图片
     *
     * @param resource
     */
    fun setLeftImageResource(resource: Int) {
        mLeftImage?.setImageResource(resource)
    }

    /**
     * 设置中间的标题文字
     *
     * @param text
     */
    fun setCenterText(text: String?) {
        mCenterTextView?.text = text
    }

    /**
     * 设置中间的标题文字颜色
     *
     * @param color
     */
    fun setCenterColor(color: Int) {
        mCenterTextView?.setTextColor(color)
    }

    /**
     * 设置中间的标题文字大小
     *
     * @param size
     */
    fun setCenterTextSize(size: Float) {
        mCenterTextView?.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
    }

    /**
     * 设置中间的标题文字是否可见
     *
     * @param visibility
     */
    fun setCenterTextVisibility(visibility: Int) {
        mCenterTextView?.visibility = visibility
    }

    /**
     * 设置分割线是否可见
     *
     * @param visibility
     */
    fun setDividerVisibility(visibility: Int) {
        mDividerView?.visibility = visibility
    }

    /**
     * 设置TitleBar背景色
     *
     * @param color
     */
    fun setTitleBarBackground(color: Int) {
        mTitleBar?.setBackgroundColor(color)
    }

    /**
     * 设置TitleBar高度
     *
     * @param height
     */
    fun setTitleBarHeight(height: Float) {
        mTitleBar?.layoutParams?.height = height.toInt()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        removeAllViews()
    }
}